//引入axios核心库
import axios from 'axios'
//引入封装好的路由模块
import router from '../../router'
//调用axios.creat() 
let http = axios.create({
    baseURL: '/api'
})
//请求拦截
http.interceptors.request.use((req) => {
    req.headers.authorization = sessionStorage.getItem('userinfo') ? JSON.parse(sessionStorage.getItem('userinfo')).token : ''
    return req
})

//响应拦截
http.interceptors.response.use((res) => {
    if (res.data.msg == '请设置请求头,并携带验证字符串') {

        router.push('/login')

    } else if (res.data.msg == '登录已过期或访问权限受限') {
        alert(res.data.msg)
        router.push('/login')
    }

    return res.data
})
export default http